/**
 * 
 */
package poj;

import java.util.Scanner;

/**
 * @author Coddy created on May 9, 2010
 * 
 */
public class P1006
{
	public static final int pc = 23;
	public static final int ec = 28;
	public static final int ic = 33;
	private static final int gcd = 23 * 28 * 33;

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		Scanner in = new Scanner(System.in);
		int p = in.nextInt();
		int e = in.nextInt();
		int i = in.nextInt();
		int d = in.nextInt();

		int testcase = 1;
		while (-1 != p && d != -1 && e != -1 && i != -1)
		{
			int a[] = new int[3];
			a[0] = p % pc;
			a[1] = e % ec;
			a[2] = i % ic;
			int x = a[2];

			while (check(x, a))
			{
				x += ic;

			}
			x -= d;
			if (x <= 0)
				x += gcd;
			System.out.println("Case " + testcase
					+ ": the next triple peak occurs in " + x + " days.");
			testcase++;
			p = in.nextInt();
			e = in.nextInt();
			i = in.nextInt();
			d = in.nextInt();
		}
	}

	/**
	 * @param x
	 * @return
	 */
	private static boolean check(int x, int[] a)
	{
		if (x % pc == a[0] && x % ec == a[1] && x % ic == a[2])
			return false;
		return true;
	}
}
